package org.jeecg.modules.cosmetic.handler;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.cosmetic.constant.enums.IsDeleteEnum;
import org.jeecg.modules.cosmetic.entity.TEvaluator;
import org.jeecg.modules.cosmetic.model.dto.EvaluatorListDTO;
import org.jeecg.modules.cosmetic.model.vo.EvaluatorEditorVO;
import org.jeecg.modules.cosmetic.model.vo.ReqIdVO;
import org.jeecg.modules.cosmetic.service.ITEvaluatorService;
import org.jeecg.modules.cosmetic.utils.AuthInfo;
import org.jeecg.modules.cosmetic.utils.CurrentUserUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author scx
 * @date 2022/7/8 10:16 上午
 */
@Slf4j
@Service
public class EvaluatorHandler {

    @Resource
    private ITEvaluatorService evaluatorService;


    /**
     * 编辑评估人信息
     *
     * @param vo
     * @return
     */
    public Result<?> evaluatorEditor(EvaluatorEditorVO vo) {
        TEvaluator evaluator = new TEvaluator();
        evaluator.setAppraisedName(vo.getUserName());
        evaluator.setDepartment(vo.getDepart());
        evaluator.setAddress(vo.getAddress());
        evaluator.setResumeUrl(vo.getResumeUrl());
        AuthInfo authInfo = CurrentUserUtil.authInfo();
        evaluator.setCreateBy(authInfo.getUserName());
        if (vo.getId() != null) {
            TEvaluator tEvaluator = evaluatorService.getById(vo.getId());
            if (!tEvaluator.getCreateBy().equals(authInfo.getUserName())) {
                return Result.error("当前用户无权限修改");
            }
            evaluator.setId(vo.getId());
            evaluatorService.updateById(evaluator);
        } else {
            evaluator.setUpdateBy(authInfo.getUserName());
            evaluatorService.save(evaluator);
        }
        return Result.OK(evaluator.getId());
    }

    /**
     * 删除评估人信息
     *
     * @param vo
     * @return
     */
    public Result<?> deleteEvaluator(ReqIdVO vo) {
        TEvaluator tEvaluator = evaluatorService.getById(vo.getId());
        Optional.ofNullable(tEvaluator).orElseThrow(() -> new JeecgBootException("评估人信息不存在"));
        AuthInfo authInfo = CurrentUserUtil.authInfo();
        if (!tEvaluator.getCreateBy().equals(authInfo.getUserName())) {
            return Result.error("当前用户无权限删除");
        }
        tEvaluator.setStatus(IsDeleteEnum.DELETE.getCode());
        tEvaluator.setUpdateBy(authInfo.getUserName());
        tEvaluator.setUpdateTime(new Date());
        boolean flag = evaluatorService.updateById(tEvaluator);
        return flag ? Result.OK() : Result.error("删除失败");
    }

    /**
     * 查询评估人列表
     *
     * @return
     */
    public Result<List<EvaluatorListDTO>> queryEvaluatorList() {
        List<TEvaluator> evaluators = evaluatorService.list(new LambdaQueryWrapper<TEvaluator>()
                .eq(TEvaluator::getStatus, IsDeleteEnum.NORMAL.getCode())
                .orderByDesc(TEvaluator::getCreateTime));
        List<EvaluatorListDTO> evaluatorListDTOS = Lists.newArrayList();
        if (!CollectionUtils.isEmpty(evaluators)) {
            evaluators.stream().map(m -> {
                EvaluatorListDTO evaluatorListDTO = new EvaluatorListDTO();
                evaluatorListDTO.setId(m.getId());
                evaluatorListDTO.setUserName(m.getAppraisedName());
                evaluatorListDTO.setDepart(m.getDepartment());
                evaluatorListDTO.setAddress(m.getAddress());
                evaluatorListDTO.setResumeUrl(m.getResumeUrl());
                return evaluatorListDTO;
            }).collect(Collectors.toCollection(() -> evaluatorListDTOS));
        }
        return Result.OK(evaluatorListDTOS);
    }

}
